package com.xbongbong.pro.rabbitmq.consumer;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.message.pojo.dto.AutoClueAssignmentRabbitMqDTO;
import com.xbongbong.pro.message.pojo.vo.AutoClueAssignmentRabbitMqVO;
import com.xbongbong.pro.rabbitmq.binders.AutoClueAssignmentConsumerBinder;
import com.xbongbong.pro.rabbitmq.constant.AutoClueAssignmentBinderConstant;
import com.xbongbong.pro.rabbitmq.feign.AutoClueAssignmentFeignClient;
import feign.RetryableException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.context.i18n.LocaleContextHolder;

import javax.annotation.Resource;
import java.util.Locale;

/**
 * @author long
 * @version v1.0
 * @date 2021/1/18 10:59
 * @since v1.0
 */
@EnableBinding(AutoClueAssignmentConsumerBinder.class)
public class AutoClueAssignmentListener {

    private static final Logger logger = LoggerFactory.getLogger(AutoClueAssignmentListener.class);

    @Resource
    private AutoClueAssignmentFeignClient autoClueAssignmentFeignClient;

    @StreamListener(value = AutoClueAssignmentBinderConstant.AUTO_CLUE_ASSIGNMENT_INPUT)
    public void autoClueAssignment(AutoClueAssignmentRabbitMqDTO autoClueAssignmentRabbitMqDTO) {
        XbbResponse<AutoClueAssignmentRabbitMqVO> response;
        try {
            Locale locale = LocaleContextHolder.getLocale();
            String lang = locale.toString();
            response = autoClueAssignmentFeignClient.autoClueAssignment(autoClueAssignmentRabbitMqDTO, lang);
            logger.info("autoClueAssignment message=" + JSON.toJSONString(autoClueAssignmentRabbitMqDTO) + "\n response=" + JSON.toJSONString(response));
        } catch (RetryableException e) {
            logger.error("AutoClueAssignmentListener.autoClueAssignment RetryableException case by Read timed out");
        } catch (Exception e) {
            logger.error("AutoClueAssignmentListener.autoClueAssignment Exception", e);
        }

    }
}
